package com.yuchen.bearrecord.repository;

import com.yuchen.bearrecord.dto.UserOpenidDto;
import com.yuchen.bearrecord.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface UserRepository extends JpaRepository<User, Long> {
    User findByOpenid(String openid);

    @Query("SELECT new com.yuchen.bearrecord.dto.UserOpenidDto(u) FROM User u WHERE u.openid <> :excludeOpenid and u.type <> 2")
    List<UserOpenidDto> findAllExcept(@Param("excludeOpenid") String excludeOpenid);

    @Query("SELECT new com.yuchen.bearrecord.dto.UserOpenidDto(u) FROM User u WHERE u.type = 0")
    List<UserOpenidDto> findUsers();

    @Transactional
    void deleteByOpenid(String openid);

}
